package com.nbsaas.boot.app.data.entity;

import com.nbsaas.boot.code.annotation.*;
import com.nbsaas.boot.jpa.data.entity.CatalogEntity;
import com.nbsaas.boot.rest.filter.Operator;
import lombok.Data;
import org.hibernate.annotations.Comment;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@SearchBean(items = {
        @SearchItem(label = "应用key", name = "appKey", key = "app.appKey", operator = Operator.eq),
        @SearchItem(label = "层级", name = "depth", key = "depth", operator = Operator.eq, classType = Integer.class),


})
@CatalogClass
@CreateByUser
@ComposeView
@FormAnnotation(title = "菜单管理", model = "菜单", menu = "1,54,56")
@Data
@Entity
@org.hibernate.annotations.Table(appliesTo = "sys_app_menu", comment = "应用菜单")
@Table(name = "sys_app_menu")
public class AppMenu extends CatalogEntity {


    @SearchItem(label = "菜单类型", name = "catalog", operator = Operator.eq, classType = Integer.class)
    @Comment("菜单类型 1菜单夹 2菜单 3按钮 4权限")
    private Integer catalog;

    @OrderBy("sortNum asc")
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    private List<AppMenu> children;

    @Comment("图标")
    private String icon;

    @Comment("禁用图标")
    private String disableIcon;


    private Long num;

    @SearchItem(label = "父菜单id", name = "parentId", key = "parent.id", operator = Operator.eq, classType = Long.class)
    @FieldConvert
    @FieldName
    @Comment("父菜单id")
    @JoinColumn(name = "parent_id")
    @ManyToOne(fetch = FetchType.LAZY)
    private AppMenu parent;

    @Comment("路径")
    private String path;


    @Comment("路由")
    private String router;

    @Comment("权限")
    private String permission;

    @Comment("创建人id")
    private Long creator;


    @JoinColumn(name = "app_id")
    @Comment("应用")
    @SearchItem(label = "应用", name = "app", key = "app.id", operator = Operator.eq, classType = Long.class)
    @FieldName
    @FieldConvert
    @ManyToOne(fetch = FetchType.LAZY)
    private App app;


    @SearchItem(label = "菜单类型", name = "menuType", operator = Operator.eq, classType = Integer.class)
    @Comment("菜单类型 1系统 2租户")
    private Integer menuType;

    @SearchItem(label = "搜索类型", name = "searchType", operator = Operator.eq, classType = Integer.class)
    @Comment("搜索类型 1可以搜索 2不可搜索")
    private Integer searchType;

    @Comment("租户id")
    private Long tenantId;

    @Override
    public Serializable getParentId() {
        if (parent != null) {
            return parent.getId();
        }
        return null;
    }
}
